package com.hxht.cmp.service.impl;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.Court;
import com.hxht.cmp.mapper.PlanMapper;
import com.hxht.cmp.mapper.TrialMapper;
import com.hxht.cmp.pojo.BasisCourt;
import com.hxht.cmp.service.StatisticalService;
import javafx.beans.binding.ObjectBinding;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 统计相关
 */
@Service
public class StatisticalServiceImpl implements StatisticalService {

    @Resource
    private PlanMapper planMapper;
    @Resource
    private TrialMapper trialMapper;

    @Override
    public int getCountCourtByStatus(String courtUid, Integer status, String today) {
        List<String> courtUidList = getCourtUidList(courtUid);
        Map<String, Object> map = new HashMap<>();
        map.put("courtUidList", courtUidList);
        map.put("status", status);
        map.put("today", today);
        int num = planMapper.getCountCourtByStatus(map);
        return num;
    }

    @Override
    public int getCountOpenCourtNum(String courtUid) {
        List<String> courtUidList = getCourtUidList(courtUid);
        int trialNum = planMapper.getCountOpenCourtNum(courtUidList);
        return trialNum;
    }

    @Override
    public int getSingleCountOpenCourtNum(String courtUid) {
        List<String> courtUidList = new ArrayList<>();
        courtUidList.add(courtUid);
        int trialNum = planMapper.getCountOpenCourtNum(courtUidList);
        return trialNum;
    }

    @Override
    public Map<String, Integer> getCountCourtMap(String courtUid) {
        List<String> courtUidList = getCourtUidList(courtUid);
        Map<String, Integer> countMap = new HashMap<>();
        int allCourtNum = 0;
        int technicalCourtNum = 0;
        int thirdCourtNum = 0;
        if (!ObjectUtils.isEmpty(courtUidList)) {
            for (String cId : courtUidList) {
                for (BasisCourt basisCourt : Constants.COURT_CONFIG_LIST) {
                    if (cId.equals(basisCourt.getUid())) {
                        allCourtNum += basisCourt.getAllCourtNum();
                        technicalCourtNum += basisCourt.getTechnicalCourtNum();
                        thirdCourtNum += basisCourt.getThirdCourtNum();
                    }
                }
            }
        } else {
            for (BasisCourt basisCourt : Constants.COURT_CONFIG_LIST) {
                allCourtNum += basisCourt.getAllCourtNum();
                technicalCourtNum += basisCourt.getTechnicalCourtNum();
                thirdCourtNum += basisCourt.getThirdCourtNum();
            }
        }

        countMap.put("allCourtNum", allCourtNum);
        countMap.put("technicalCourtNum", technicalCourtNum);
        countMap.put("thirdCourtNum", thirdCourtNum);
        return countMap;
    }

    @Override
    public int getCountFileSize(String courtUid) {
        List<String> courtUidList = getCourtUidList(courtUid);
        int fileSizeTotal = planMapper.getCountFileSize(courtUidList);
        return fileSizeTotal;
    }

    @Override
    public int getSingleCountFileSize(String courtUid) {
        List<String> courtUidList = new ArrayList<>();
        courtUidList.add(courtUid);
        int fileSizeTotal = planMapper.getCountFileSize(courtUidList);
        return fileSizeTotal;
    }

    @Override
    public int getCountOpenCourtTime(String courtUid) {
        List<String> courtUidList = getCourtUidList(courtUid);
        int countTime = trialMapper.getCountOpenCourtTime(courtUidList);
        return countTime;
    }

    @Override
    public int getSingleCountOpenCourtTime(String courtUid) {
        List<String> courtUidList = new ArrayList<>();
        courtUidList.add(courtUid);
        int countTime = trialMapper.getCountOpenCourtTime(courtUidList);
        return countTime;
    }

    @Override
    public List<Map<String, Object>> selectListJudgeOpenTimeAndOpenNum(String courtUid, String startDate, String endDate) {
        Map<String, String> param = new HashMap<>();
        param.put("courtUid", courtUid);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        //获取开庭数
        List<Map<String, Object>> openNums = planMapper.selectListJudgeOpenNum(param);
        //获取开庭时长
        List<Map<String, Object>> openTimes = trialMapper.selectListJudgeOpenTime(param);
        //结果
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> openNum : openNums) {
            for (Map<String, Object> openTime : openTimes) {
                if (Objects.equals(openNum.get("userUid"), openTime.get("userUid"))) {
                    Map<String, Object> resultMap = new HashMap<>();
                    if (ObjectUtils.isEmpty(openNum.get("realName"))) {
                        continue;
                    }
                    for (BasisCourt basisCourt : Constants.COURT_CONFIG_LIST) {
                        if (basisCourt.getUid().equals(openNum.get("courtUid"))) {
                            resultMap.put("courtName", basisCourt.getAbbreviation());
                        }
                    }
                    resultMap.put("userUid", openNum.get("userUid"));
                    resultMap.put("realName", openNum.get("realName"));
                    resultMap.put("courtUid", openNum.get("courtUid"));
                    resultMap.put("openNum", openNum.get("num"));
                    resultMap.put("openTime", openTime.get("timeTotal"));
                    result.add(resultMap);
                }
            }
        }
        return result;
    }

    private static List<String> getCourtUidList(String courtUid) {
        Court nowCourt = Constants.COURT_MAP.get(courtUid);
        List<String> courtCodes = new ArrayList<>();
        //传入courtUid与当前项目中的courtUid不一致获取其下级法院courtUid
        if (!Objects.equals(courtUid, Constants.CENTRAL_COURT.getUid())) {
            for (Court court : Constants.COURT_LIST) {
                if (Objects.equals(courtUid, court.getUid()) || Objects.equals(nowCourt.getUid(), court.getParentUid())) {
                    courtCodes.add(court.getUid());
                }
            }
        }
        return courtCodes;
    }
}
